#include "../my_lvgl_apps.h"





// Arc callback
static void Arc_event_handler(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if( code == LV_EVENT_VALUE_CHANGED ){
        // printf("Set brightness: %d", lv_arc_get_value(obj));
        int16_t value = lv_arc_get_value(obj);
        // Set LCD brightness
        M5.Axp.SetLcdVoltage(map(value, 0, 100, 2500, 3300));
        Serial.println(value);
    }
}

// Label_back callback
static void btn_back_event_handler(lv_event_t * e)
{
    // lv_obj_t * obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if( code == LV_EVENT_CLICKED ){
        // printf("Go back");
        Serial.println("Back");
    }
}



void app_setting_LCD_Bri(void)
{
    // create and load screen
    lv_obj_t * scr = lv_obj_create(NULL);
    lv_scr_load(scr);
    // create Arc widget
    lv_obj_t * arc = lv_arc_create(lv_scr_act());
    lv_obj_set_size(arc, 180, 180);
    lv_arc_set_rotation(arc, 135);
    lv_arc_set_bg_angles(arc, 0, 270);
    lv_arc_set_value(arc, 40);
    // lv_obj_center(arc);
    lv_obj_align(arc, LV_ALIGN_CENTER, 0, 10);
    // add Arc callback
    lv_obj_add_event_cb(arc, Arc_event_handler, LV_EVENT_ALL, NULL);

    // create Lable "BRIGHTNESS"
    lv_obj_t * label = lv_label_create(arc);
    lv_label_set_text(label, "BRIGHTNESS");
    lv_obj_center(label);


    // create btn style
    static lv_style_t style_btn;
    lv_style_init(&style_btn);
    // set btn transparent and text black
    lv_style_set_bg_opa(&style_btn, LV_OPA_0);
    lv_style_set_shadow_opa(&style_btn, LV_OPA_0);
    lv_style_set_text_color(&style_btn, lv_color_black());

    // create btn_back and add event callback
    lv_obj_t * btn_back = lv_btn_create(lv_scr_act());
    lv_obj_set_pos(btn_back, -15, -10);
    // give it a bigger area
    lv_obj_set_width(btn_back, 60);
    lv_obj_set_height(btn_back, 50);
    // setup callback
    lv_obj_add_event_cb(btn_back, btn_back_event_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_style(btn_back, &style_btn, 0);
    // create btn_back label
    lv_obj_t * label_back = lv_label_create(btn_back);
    lv_label_set_text(label_back, "<<<");
    lv_obj_align_to(btn_back, label_back, LV_ALIGN_TOP_LEFT, 0, 0);

}
